﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net.NetworkInformation;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY.iCalManager
{
    /// <summary>
    /// Logique d'interaction pour ICalSavWindows.xaml
    /// </summary>
    public partial class ICalSavWindows : Window
    {
        public dbDataContext db_remote;
        public Local_db db_local;

        public ICalSavWindows()
        {
            InitializeComponent();
            fill_calendar_list();
            load_campus_comboBox();

            date_begin_datePicker.Text = DateTime.Now.ToString();
            date_end_datePicker.Text = DateTime.Now.ToString();


        SingletonTokenUser token_temp = SingletonTokenUser.GetInstance();

        if (token_temp.Database_mode == "online")
        {
            db_remote = new dbDataContext();
        }

        else if (token_temp.Database_mode == "offline")
        {
            db_local = new Local_db("Data Source=|DataDirectory|\\local_db.sdf;Max Database Size=2047");
        }


        }

        #region fonctions


        private void fill_calendar_list()
        {
            dbDataContext db_remote = new dbDataContext();

            SingletonTokenUser token = SingletonTokenUser.GetInstance();
            calendar_listBox.Items.Clear();

            calendar_listBox.Items.Add("Personal");


            if ((string)campus_comboBox.SelectedValue != null)
            {
                int id_camp = (from e in db_remote.campus where e.name == campus_comboBox.SelectedValue.ToString() select e.id).FirstOrDefault();

                foreach (classes s in (from e in db_remote.classes
                                       where e.campus_id == id_camp
                                       select e))
                {
                    string cursus_name = (from i in db_remote.cursus where i.id == s.cursus_id select i).FirstOrDefault().name;

                    calendar_listBox.Items.Add(campus_comboBox.SelectedValue + " - " + cursus_name + " - " + s.name);
                }
            }

        }

        private void fill_calendar_list_offline()
        {
            Local_db db_local = new Local_db("Data Source=|DataDirectory|\\local_db.sdf;Max Database Size=2047");

            SingletonTokenUser token = SingletonTokenUser.GetInstance();
            calendar_listBox.Items.Clear();

            calendar_listBox.Items.Add("Personal");


            if ((string)campus_comboBox.SelectedValue != null)
            {
                int id_camp = (from e in db_local.Campus where e.Name == campus_comboBox.SelectedValue.ToString() select e.Id).FirstOrDefault();

                foreach (Classes s in (from e in db_local.Classes
                                       where e.Campus_id == id_camp
                                       select e))
                {
                    string cursus_name = (from i in db_local.Cursus where i.Id == s.Cursus_id select i).FirstOrDefault().Name;

                    calendar_listBox.Items.Add(campus_comboBox.SelectedValue + " - " + cursus_name + " - " + s.Name);
                }
            }

        }

        private void load_campus_comboBox()
        {
            SingletonTokenUser token = SingletonTokenUser.GetInstance();

            campus_comboBox.Items.Clear();

            dbDataContext db_remote = new dbDataContext();

            if (token.User_level == SingletonTokenUser.Admin) 
            {
                foreach (campus s in (from e in db_remote.campus select e))
                {
                    campus_comboBox.Items.Add(s.name);
                }
            }
            else if (token.User_level == SingletonTokenUser.Manager)
            {

                campus_comboBox.Items.Clear();

                //Récupération, découpage et affichage des campus enregistrés pour un manager
                string[] table_campus;
                string registred_campus = token.User_campus;
                table_campus = registred_campus.Split('|');

                for (int i = 0; i < table_campus.Length; i++)
                {
                    campus_comboBox.Items.Add(table_campus[i]);
                }

                campus_comboBox.SelectedItem = campus_comboBox.Items[0];



            }
            else if (token.User_level == SingletonTokenUser.Teacher || token.User_level == SingletonTokenUser.Student)
            {
                campus_comboBox.Items.Add(token.User_campus);
                campus_comboBox.IsEnabled = false;
            }            
        }

        private void load_campus_comboBox_offline()
        {
            SingletonTokenUser token = SingletonTokenUser.GetInstance();

            campus_comboBox.Items.Clear();

            if (token.User_level == SingletonTokenUser.Admin)
            {
                foreach (Campus s in (from e in db_local.Campus select e))
                {
                    campus_comboBox.Items.Add(s.Name);
                }
            }
            else if (token.User_level == SingletonTokenUser.Manager)
            {
                //Dépendra de ce qui sera fais pour la sélection des plusieurs campus à un manager
            }
            else if (token.User_level == SingletonTokenUser.Teacher || token.User_level == SingletonTokenUser.Student)
            {
                campus_comboBox.Items.Add(token.User_campus);
                campus_comboBox.IsEnabled = false;
            }
        }

        #endregion

        #region controles

        private void export_button_Click(object sender, RoutedEventArgs e)
        {
            if (calendar_listBox.SelectedIndex >= 0) 
            {
                if((DateTime)date_begin_datePicker.SelectedDate < (DateTime)date_end_datePicker.SelectedDate) {
                    ICalManager iCal = new ICalManager((DateTime)date_begin_datePicker.SelectedDate, (DateTime)date_end_datePicker.SelectedDate, calendar_listBox);
                    Close();
                } 
                else 
                {
                    MessageBox.Show("The begin date must be before the end date");
                } 
            }
            else
            {
                 MessageBox.Show("No calendar selected");
            }

            
        }

        private void cancel_button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void campus_comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            fill_calendar_list();
        }

        #endregion
    }
}